// 启动屏幕
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:go_router/go_router.dart';
import 'package:hive/hive.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});
  @override
  State<SplashPage> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    _checkLoginStatus();
  }
  Future<void> _checkLoginStatus() async {
    // 模拟启动屏延迟
    await Future.delayed(const Duration(seconds: 2));

    // 检查登录状态


    var box = Hive.box('loginBox');
    String isLoggedIn = box.get('auth_token', defaultValue: "");


    if (!mounted) return;
    if (isLoggedIn.isNotEmpty && isLoggedIn != "" && isLoggedIn != "null" && isLoggedIn != null) {
      // 已登录,跳转到首页
      context.go('/mainhome');
    } else {
      // 未登录,跳转到登录页
      context.go('/login');
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/logo.png',
              width: 120,
              height: 120,
            ),
            const SizedBox(height: 24),
            const Text(
              '欢迎使用',
              style: TextStyle(
                fontSize: 36,
                fontWeight: FontWeight.bold,
                color: Colors.black87,
              ),
            ),
            // const CircularProgressIndicator(),
            SpinKitFadingCircle(
              color: Colors.red,
              size: 50.0,
            ),
          ],
        ),
      ),
    );
  }
}